Visualización por estratos:#
Trabajaremos con el conjunto de datos de Google Play Store Apps alojado en googleplaystore.csv. Su tarea es crear una visualización con:
Un gráfico de barras de un número de aplicaciones estratificado por cada categoría Content Rating (calificado por Everyone/Teen).
Un mapa de calor que indica el número de aplicaciones estratificadas por app Category y rangos de rangos segmentados por Rating. El usuario debe poder interactuar con el gráfico seleccionando cualquiera de los tipos de Content Rating y el cambio correspondiente debería reflejarse en el mapa de calor para incluir sólo el número de aplicaciones en la categoría Content Rating.
## Instalación de librerías
import altair as alt
import pandas as pd
Descripción de los datos#
Este conjunto de datos consta de datos extraídos de la web de más de 10.000 aplicaciones de Google Play Store. Contiene datos sobre las aplicaciones, como la categoría, el número de instalaciones y el precio.

Manejo de datos#
## Descarga del conjunto de datos googleplaystore.csv. Formato pandas DataFrame
df = pd.read_csv("https://raw.githubusercontent.com/lihkir/Uninorte/main/AppliedStatisticMS/DataVisualizationRPython/Lectures/Python/PythonDataSets/googleplaystore.csv")
df.head()
| App | Category | Rating | Reviews | Size | Installs | Type | Price | Content Rating | Genres | Last Updated | Current Ver | Android Ver | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Photo Editor & Candy Camera & Grid & ScrapBook | ART_AND_DESIGN | 4.1 | 159 | 19M | 10,000+ | Free | 0 | Everyone | Art & Design | January 7, 2018 | 1.0.0 | 4.0.3 and up |
| 1 | Coloring book moana | ART_AND_DESIGN | 3.9 | 967 | 14M | 500,000+ | Free | 0 | Everyone | Art & Design;Pretend Play | January 15, 2018 | 2.0.0 | 4.0.3 and up |
| 2 | U Launcher Lite – FREE Live Cool Themes, Hide ... | ART_AND_DESIGN | 4.7 | 87510 | 8.7M | 5,000,000+ | Free | 0 | Everyone | Art & Design | August 1, 2018 | 1.2.4 | 4.0.3 and up |
| 3 | Sketch - Draw & Paint | ART_AND_DESIGN | 4.5 | 215644 | 25M | 50,000,000+ | Free | 0 | Teen | Art & Design | June 8, 2018 | Varies with device | 4.2 and up |
| 4 | Pixel Draw - Number Art Coloring Book | ART_AND_DESIGN | 4.3 | 967 | 2.8M | 100,000+ | Free | 0 | Everyone | Art & Design;Creativity | June 20, 2018 | 1.1 | 4.4 and up |
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10841 entries, 0 to 10840
Data columns (total 13 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 App 10841 non-null object
1 Category 10841 non-null object
2 Rating 9367 non-null float64
3 Reviews 10841 non-null object
4 Size 10841 non-null object
5 Installs 10841 non-null object
6 Type 10840 non-null object
7 Price 10841 non-null object
8 Content Rating 10840 non-null object
9 Genres 10841 non-null object
10 Last Updated 10841 non-null object
11 Current Ver 10833 non-null object
12 Android Ver 10838 non-null object
dtypes: float64(1), object(12)
memory usage: 1.1+ MB
na_count = df.isna().sum()
print(na_count)
App 0
Category 0
Rating 1474
Reviews 0
Size 0
Installs 0
Type 1
Price 0
Content Rating 1
Genres 0
Last Updated 0
Current Ver 8
Android Ver 3
dtype: int64
## Eliminación de las entradas del DataFrame que tienen valores de característica de NA.
df_na = df.dropna()
df_na.head()
| App | Category | Rating | Reviews | Size | Installs | Type | Price | Content Rating | Genres | Last Updated | Current Ver | Android Ver | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Photo Editor & Candy Camera & Grid & ScrapBook | ART_AND_DESIGN | 4.1 | 159 | 19M | 10,000+ | Free | 0 | Everyone | Art & Design | January 7, 2018 | 1.0.0 | 4.0.3 and up |
| 1 | Coloring book moana | ART_AND_DESIGN | 3.9 | 967 | 14M | 500,000+ | Free | 0 | Everyone | Art & Design;Pretend Play | January 15, 2018 | 2.0.0 | 4.0.3 and up |
| 2 | U Launcher Lite – FREE Live Cool Themes, Hide ... | ART_AND_DESIGN | 4.7 | 87510 | 8.7M | 5,000,000+ | Free | 0 | Everyone | Art & Design | August 1, 2018 | 1.2.4 | 4.0.3 and up |
| 3 | Sketch - Draw & Paint | ART_AND_DESIGN | 4.5 | 215644 | 25M | 50,000,000+ | Free | 0 | Teen | Art & Design | June 8, 2018 | Varies with device | 4.2 and up |
| 4 | Pixel Draw - Number Art Coloring Book | ART_AND_DESIGN | 4.3 | 967 | 2.8M | 100,000+ | Free | 0 | Everyone | Art & Design;Creativity | June 20, 2018 | 1.1 | 4.4 and up |
# Aquí se comprueba la eliminación de NA en el dataFrame.
df_na_count = df_na.isna().sum()
print(df_na_count)
App 0
Category 0
Rating 0
Reviews 0
Size 0
Installs 0
Type 0
Price 0
Content Rating 0
Genres 0
Last Updated 0
Current Ver 0
Android Ver 0
dtype: int64
Gráfico de barras (In/Zoom-Out)#
## Ahora creamos el gráfico de barras necesario del número de aplicaciones en cada categoría Content Rating
alt.data_transformers.disable_max_rows()
bar_chart = alt.Chart(df_na).mark_bar().encode(
x='Content Rating',
y='count()',
).properties(width=350).interactive()
bar_chart
df_na['Content Rating'].value_counts(normalize=True)
Content Rating
Everyone 0.792094
Teen 0.115812
Mature 17+ 0.049252
Everyone 10+ 0.042415
Adults only 18+ 0.000321
Unrated 0.000107
Name: proportion, dtype: float64
De acuerdo con los registros, más de 7000 de un total de 10841 aplicaciones (79%) tienen contenido apto para todo público. Las aplicaciones restantes están específicamente diseñadas para mayores de 10 y 17 años (4.2% y 4.9%) y adolescentes(11.6%). Revistas diseñadas para adultos solamente o sin clasificación ocupan un porcentaje muy pequeño de las observaciones.
Heatmap con círculos#
# Creamos el mapa de calor
import altair as alt
alt.data_transformers.disable_max_rows()
# Crear el gráfico de calor
heatmap = alt.Chart(df_na).mark_rect().encode(
alt.X('Category:N'),
alt.Y('Rating:Q', bin=True),
color=alt.Color('count()', scale=alt.Scale( domain=[0, 100], scheme='greenblue'), legend=alt.Legend(title='Número de Aplicaciones')),
tooltip=['Category', 'count()']
).properties(width=600).interactive()
heatmap
circles = heatmap.mark_point().encode(
alt.ColorValue('gray'),
alt.Size('count()', legend=alt.Legend(title='Registros seleccionados'))
)
heatmap + circles
El análisis gráfico de las observaciones sugiere que el mayor porcentaje de aplicaciones se sitúa en la categoría FAMILY (18.65%), la mayoría de ellas con niveles de aprobación entre 3.5 y 5, por parte de sus usuarios.
Combinamos el código para colocar el gráfico de barras y el mapa de calor uno al lado del otro utilizando la función bars | heatmap
bar_chart = alt.Chart(df_na).mark_bar().encode(
x='Content Rating',
y='count()',
).properties(width=350).interactive()
heatmap = alt.Chart(df_na).mark_rect().encode(
alt.X('Category:N'),
alt.Y('Rating:Q', bin=True),
color=alt.Color('count()', scale=alt.Scale( domain=[0, 100], scheme='greenblue'), legend=alt.Legend(title='Número de Aplicaciones'))
).properties(width=350).interactive()
bar_chart | heatmap
Vincular dinámicamente un gráfico de barras y un mapa de calor#
Seleccionar la categoría mediante el método de Selection. Agregamos la opción de tooltip para visualizar el conteo de aplicaciones a medida que el cursos de desplaza por la gráfica.
selected_category = alt.selection_point(encodings=['x'])
# Crear el gráfico de calor
heatmap = alt.Chart(df_na).mark_rect().encode(
alt.X('Category:N'),
alt.Y('Rating:Q', bin=True),
color=alt.Color('count()', scale=alt.Scale( domain=[0, 100], scheme='greenblue'), legend=alt.Legend(title='Número de Aplicaciones')),
tooltip=['Category', 'count()']
).properties(width=350).interactive()
heatmap
Colocar los círculos al mapa de calor
circles = heatmap.mark_point().encode(
alt.ColorValue('gray'),
alt.Size('count()', legend=alt.Legend(title='Registros seleccionados'))
).transform_filter(
selected_category
)
Utilizmos la función heatmap+circles | bars_chart para vincular dinámicamente el gráfico de barras y el mapa de calor
bar_chart = alt.Chart(df_na).mark_bar().encode(
x='Content Rating',
y='count()',
color = alt.condition(selected_category, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
).properties(width=450).add_params(selected_category)
heatmap + circles | bar_chart
Este gráfico nos brinda la opotunidad de visualizar el número de aplicaciones que están destinadas a públicos diversos, las categorías existentes y las calificaciones para cada una de ellas. Es una representación de utilidad para quienes estén interesados en explorar la calidad del servicio y las preferencias de los usuarios por cada categoría de aplicaciones.